package org.sapp;

public class PersistencyParameters 
{
	private String dbName;
	private String dbHost;
	private int dbPort;	
	private String dbUserName;
	private String dbPassword;
	private String dbDriver;
	private String jdbcUrl;
	private String dbDriverClassName;
	
	public String getDBName()
	{
		return dbName;
	}
	
	public String getDBHost()
	{
		return dbHost;
	}
	
	public int getDBPort()
	{
		return dbPort;
	}
	
	public String getDBUserName()
	{
		return dbUserName;
	}
	
	public String getDBPassword()
	{
		return dbPassword;
	}
	
	public String getDBDriver()
	{
		return dbDriver;
	}
	
	public String getDBDriverClassName()
	{
		return dbDriverClassName;
	}
	
	public void setJdbcUrl(String url) throws Exception
	{
		this.jdbcUrl = url;
		
		int pos,prev_pos;
		String substr;
		
		// Parse protocol
		pos = url.indexOf(":");
		if (pos==-1)
		{
			// TODO Something worng. Raise exception.
			return;			
		}
		
		substr = url.substring(0, pos);
		if (!substr.equals("jdbc"))
		{
			// TODO Something worng. Raise exception.
			return;
		}
		prev_pos = pos;
		
		// Parse driver name
		pos = url.indexOf(":", prev_pos+1);
		if (pos==-1)
		{
			// TODO Something worng. Raise exception.
			return;			
		}
				
		substr = url.substring(prev_pos+1, pos);
		dbDriver = substr;

		prev_pos = pos;		
		
		// Parse Host
		pos = url.indexOf("//", prev_pos+1);		
		if (pos==-1)
		{
			// TODO Something worng. Raise exception.
			return;			
		}
		
		prev_pos = pos;	
		
		pos = url.indexOf(":", prev_pos+1);		
		if (pos==-1)
		{
			// TODO Something worng. Raise exception.
			return;			
		}
		
		substr = url.substring(prev_pos+2, pos);
		dbHost = substr;		
		
		prev_pos = pos;		

		pos = url.indexOf("/", prev_pos+1);		
		if (pos==-1)
		{
			// TODO Something worng. Raise exception.
			return;			
		}		
		
		substr = url.substring(prev_pos+1, pos);
		dbPort = Integer.valueOf(substr);
		
		prev_pos = pos;		
		
		substr = url.substring(prev_pos+1);		
		dbName = substr;
		
		//URL jdbcUrl = new URL(url);
		//dbHost = jdbcUrl.getHost();
	}
	
	public void setDBUserName(String username)
	{
		this.dbUserName = username;
	}
	
	public void setDBPassword(String password)
	{
		this.dbPassword = password;
	}
	
	public void setDBDriverClassName(String className)
	{
		dbDriverClassName = className;
	}
	
}
